package com.example.xiaodu.yuejian12.Fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.example.xiaodu.yuejian12.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment{

    private RadioButton activity,master,game;
    private ActivityFragment activityFragment;
    private MasterFragment masterFragment;
    private GameFragment gameFragment;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

    public HomeFragment() {
        // Required empty public constructor
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        activity=view.findViewById(R.id.radio_button1);
        master=view.findViewById(R.id.radio_button2);
        game=view.findViewById(R.id.radio_button3);

        activity.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    if(activityFragment==null){
                        activityFragment=new ActivityFragment();
                    }
                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.framelayout,activityFragment);
                    fragmentTransaction.commit();
                }
            }
        });


        master.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    if(masterFragment==null){
                        masterFragment =new MasterFragment();
                    }
                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.framelayout,masterFragment);
                    fragmentTransaction.commit();
                }
            }
        });

        game.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    if(gameFragment==null){
                        gameFragment =new GameFragment();
                    }
                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.framelayout,gameFragment);
                    fragmentTransaction.commit();
                }
            }
        });





        activity.setChecked(true);
    }

    public void onAttach(Context context) {
        super.onAttach(context);
        fragmentManager= getChildFragmentManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

}
